import logging
from logging.config import dictConfig
from logging.handlers import RotatingFileHandler

import os
import yaml

#创建logs文件夹
logs_dir = os.path.join(os.path.curdir, "logs")
if os.path.exists(logs_dir) and os.path.isdir(logs_dir):
    pass
else:
    os.mkdir(logs_dir)

class Log:

    @staticmethod
    def logger():
        logger = logging.getLogger("")
        if len(logger.handlers) == 0:
            # define a rotating file handler
            rotatingFileHandler = logging.handlers.RotatingFileHandler(filename="./logs/log.txt",
                                                                       maxBytes=1024 * 1024 * 50,
                                                                       backupCount=5)

            formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s -%(module)s %(message)s")
            rotatingFileHandler.setFormatter(formatter)
            logging.getLogger("").addHandler(rotatingFileHandler)

            # define a handler whitch writes messages to sys
            console = logging.StreamHandler()
            console.setLevel(logging.NOTSET)

            # set a format which is simple for console use
            formatter = logging.Formatter("%(name)-12s: %(levelname)-8s  -%(module)s %(message)s")

            # tell the handler to use this format
            console.setFormatter(formatter)

            # add the handler to the root logger
            logger.addHandler(console)

            # set initial log level
            logger.setLevel(logging.NOTSET)
        return logger;

    @staticmethod
    def debug(msg):
        Log.logger().debug(msg)

    @staticmethod
    def info(msg):
        Log.logger().info(msg)

    @staticmethod
    def warning(msg):
        Log.logger().warning(msg)

    @staticmethod
    def error(msg):
        Log.logger().error(msg)
